<?php
class Administrator_MlmController extends Zend_Controller_Action
{
	public function init()
	{
		$member=new CMember();
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
			$this->_redirect(MY_URL.'/administrator-auth-login');
		}
		
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		
	}
	
	public function cashAction()
	{
		$cashModel = new CMlmcashlog();
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 10;
		
		$type = $this->getRequest()->getParam('type', 5);
		
		$cashList = $cashModel->getAll($page, $pageNum, $type);
		$pageCount = $cashModel->getPageCount($pageNum, $type);
		$statistics = new CStatistics();        
        
        $param = 'administrator-mlm-cash?type=' . $type . '&';
        $pageLink = $statistics->orderPage($pageCount, $pageNum, $page, $param);
        
        $this->view->pageLink = $pageLink;
        $this->view->cashList = $cashList;
        
        $mlm = new CMlm();
        
        $this->view->inProcess = $mlm->getInProcessCash();
        $this->view->success = $mlm->getSuccessCash();
        $this->view->refuse = $mlm->getRefuseCash();
        
        $this->view->type = $type;
        
	}
	
	public function casheditAction()
	{
		$cashModel = new CMlmcashlog();
		$id = $this->getRequest()->getParam('id', 1);
		$cash = $cashModel->find($id)->current();
		$this->view->id = $id;
		$this->view->cash = $cash;
		
		if ($this->getRequest()->isPost()) {
			$type = $this->getRequest()->getParam('type', 0);
			$this->getHelper('layout')->disableLayout();
    		$this->getHelper('viewRenderer')->setNoRender();
			$mlm = new CMlm();	
			$mlm->doCash($id, $type);
			$this->view->showMsg('success', 'administrator-mlm-cash');	
		}		
	}
	
	public function viewAction()
	{
		$mlmMember = new CMlmmember();
		$rows = $mlmMember->fetchAll();
		$result = array();
		$mlm = new CMlm();
		foreach ($rows as $v) {
			$tmpCount = $mlm->getLevel1Uid($v->uid);
			
			if (!$tmpCount) {
				continue;
			}
			$tmp[0] = $tmpCount;
			$tmp[1] = $mlm->getLevel2Uid($v->uid);
			$tmp[2] = $mlm->getLevel3Uid($v->uid);
			$result[$v->uid] = $tmp;
		}
		$this->view->count = count($result);
		$this->view->result = $result;
	}
}